using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BL;
using BE;

public partial class MasterPage : System.Web.UI.MasterPage
{
    #region Atributos
    protected DataTable dt = null;
    #endregion

    #region Eventos
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblWelcome.Text = Resources.Resource.lblWelcome;
            lblFecha.Text = Configuracion.ObtenerFormatoFechaCompleto(DateTime.Now);

            Usuario oUsuario = (Usuario)Session[Constans.SessionUsuario.ToString()];
            //Session[Constans.SessionUsuario.ToString()]
            lblLogin.Text = oUsuario.Login;
            lblPerfil.Text = oUsuario.PerfilNombre;
            CargarMenu(oUsuario.PerfilId);            
        }
    }
    protected void mOpciones_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (e.Item.Text == Resources.Resource.lblCerrarSesion)
        {
            Session.Clear();
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");
        }        
    }
    #endregion

    #region Metodos   
    public void CargarMenu(long  uidPerfilId)
    {
        mOpciones.Items.Clear();

        dt = PerfilBO.ObtenerMenuPrincipal(uidPerfilId);
        if (dt.Rows.Count > 0)
        {            
            DataRow[] drList = dt.Select("MenuLevel=1");
            for (int i = 0; i < drList.Length; i++)
            {
                MenuItem item = new MenuItem();
                item.Value = drList[i]["MenuId"].ToString();
                item.Text = drList[i]["Nombre"].ToString();
                mOpciones.Items.Add(item);                
                GetMenuChilds(drList[i]["MenuId"].ToString(), item);                
            }            
        }
               
        MenuItem itemCerrarSesion = new MenuItem();        
        itemCerrarSesion.Text = Resources.Resource.lblCerrarSesion;
        mOpciones.Items.Add(itemCerrarSesion);
    }
    private void GetMenuChilds(String strMenuId,MenuItem item)
    {
        DataRow[] dr = dt.Select("PadreId='" + strMenuId + "'");
        for (int i = 0; i < dr.Length; i++)
        {
            MenuItem itemChild = new MenuItem();
            itemChild.Value = dr[i]["MenuId"].ToString();
            itemChild.Text = dr[i]["Nombre"].ToString();
            itemChild.NavigateUrl = dr[i]["MenuUrl"].ToString();    
            item.ChildItems.Add(itemChild);

            GetMenuChilds(dr[i]["MenuId"].ToString(),itemChild);            
        }
    }
    #endregion
}
